<template>
    <div class="login">
        <main>
            <div class="main_header">欢迎登陆</div>
            <el-form hide-required-asterisk label-position="top" :model="ruleForm" status-icon ref="ruleForm"
                     label-width="100px" class="demo-ruleForm">
                <el-form-item label="账号" prop="username">
                    <el-input type="txt" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="" prop="captcha">
                            <el-input v-model="ruleForm.captcha" placeholder="验证码"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="12">
                        <el-image style="width: 200px; height: 50px" :src="url"></el-image>
                        <el-button @click="getCaptcha()" type="text">点击刷新</el-button>
                    </el-col>
                </el-row>
                <el-form-item>
                    <el-button :loading="loadingFlag" type="primary" @click="submitForm('ruleForm')">登陆</el-button>
                </el-form-item>
            </el-form>
        </main>
    </div>
</template>

<script>
    import {fetchLogin} from "../api/index"


    export default {
        data() {
            return {
                loadingFlag: false,
                ruleForm: {
                    username: "",
                    password: "",
                    captcha: ""
                },
                rules: {
                    username: [
                        {required: true, message: "请输入账号", trigger: "blur"}
                    ],
                    password: [
                        {required: true, message: "请输入密码", trigger: "blur"}
                    ],
                    captcha: [
                        {required: true, message: "请输入验证码", trigger: "blur"}
                    ]
                },
                url: 'http://127.0.0.1:9191/ddkbishe/captcha.jpg'
            }
        },
        methods: {
            submitForm(formName) {

                console.log(formName);
                this.$refs[formName].validate((valid) => {
                    if (this.ruleForm === null) {
                        this.$message({
                            message: "请输入账号或密码",
                            type: "warning",
                            center: true
                        })
                    } else {
                        if (valid) {
                            this.loadingFlag = true
                            fetchLogin(this.ruleForm).then(res => {
                                if (res.code === 20000) {
                                    this.$router.push({
                                        path: '/index/shouye/shouye',
                                        query: {name: this.ruleForm.username, password: this.ruleForm.password}
                                    });
                                    this.$message({
                                        message: "登陆成功",
                                        type: "success",
                                        center: true
                                    })
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: "warning",
                                        center: true
                                    })
                                    this.loadingFlag = false;
                                   this.getCaptcha();
                                }
                            }).catch(err => {
                                this.$message({
                                    message: "请求失败",
                                    type: "warning",
                                    center: true
                                })
                                this.loadingFlag = false
                            })
                        } else {
                            this.$message({
                                message: "请输入账号或密码",
                                type: "warning",
                                center: true
                            })
                            return false;
                        }
                    }

                });
            },

            getCaptcha() {
                this.url += "?t=" + new Date().getTime()
            }
        }
    }
</script>

<style lang='less' scoped>
    .login {
        width: 100%;
        height: 100%;
        background-image: url("../assets/images/loginBanner.jpg");
        background-size: cover;
        overflow: hidden;

        main {
            .main_header {
                text-align: center;
                font-size: 24px
            }

            width: 20%;
            padding: 50px;
            background-color: #fff;
            border-radius: 5px;
            margin: 200px auto;

            .el-button--primary {
                width: 100%;
            }
        }
    }

</style>
